package com.jinunn.hosp.service.impl;

import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.atguigu.yygh.vo.order.SignInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinunn.common.exception.YyghException;
import com.jinunn.common.result.ResultCodeEnum;
import com.jinunn.hosp.mapper.HospitalSetMapper;
import com.jinunn.hosp.service.HospitalSetService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author : JinDun
 * @date : 2021/4/23 23:20
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {
    @Override
    public Page<HospitalSet> findPage(long current, long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current,limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }
        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public HospitalSet getDetil(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean updateHostset(HospitalSet hospitalSet) {
        int flag = baseMapper.updateById(hospitalSet);
        return flag > 0;
    }

    @Override
    public String getSingKey(String hoscode) {
        HospitalSet hospitalSet = baseMapper
                .selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        return hospitalSet.getSignKey();
    }

    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        HospitalSet hospitalSet = baseMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
        if (hospitalSet==null){
            throw new YyghException(ResultCodeEnum.SERVICE_ERROR);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        return signInfoVo;
    }
}
